import Log from "./Log";
import {Trigger} from "../w3ts/handles/trigger";
import {MapPlayer} from "../w3ts/handles/player";

let runtime: NoSelf | any = require('jass.runtime')
let jDebug: NoSelf = require('jass.debug')
let jass: NoSelf = require('jass.common')

export default class Develop {
    static debuggerPort = 4279
    static isSocketDebug = false

    static open() {
        //test
        runtime.debugger = Develop.debuggerPort;
        runtime.console = true;
        isDebug = true;
        if (!DzTriggerRegisterSyncData) {
            print("本地开发环境DzApi不存在，如需使用DzApi请加入QQ群941442872 群文件下载DzApi相关编辑器插件安装后即可使用！")
            DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 5, '当前环境无完整Japi！');
        }
        //de 0
        print("runtime.version=" + runtime["version"]);
        if (Develop.isSocketDebug) {
            pcall(function () {
                let dbg = require('debugger')
                dbg.io('listen:127.0.0.1:' + Develop.debuggerPort);
                dbg.start()
                PACKAGE.path = local_map_dir_path + '\\?.lua;' + PACKAGE.path
                print("启动Lua Socket调试 " + Develop.debuggerPort)
            })
        }
        //
        let trigger = new Trigger()
        trigger.registerPlayerChatEvent(MapPlayer.fromIndex(0), "d", true)
        trigger.addAction(() => {
            Develop.showDebugInfo();
        })
        //
        let trigger2 = new Trigger()
        trigger2.registerPlayerChatEvent(MapPlayer.fromIndex(0), "stop", true)
        trigger2.addAction(() => {
            log.debug('暂停Lua垃圾回收:' + collectgarbage('stop'))
        })

    }

    static close() {
        runtime.console = false;
        isDebug = false;
        Log.enable = false;
        Log.enablePrint = false;
    }


    static showDebugInfo() {
        log.debug("========Develop.showJassDebugInfo():Start========")
        log.debug('垃圾收集器在运行为：', collectgarbage('isrunning'))
        let lua_memory = collectgarbage('count')
        // @ts-ignore
        let stringTs: NoSelf = string
        let lm = stringTs.format("%.3f", (lua_memory / 1024));
        log.debug('脚本引擎占用内存[' + lm + 'm]')
        collectgarbage('collect')
        log.debug("jDebug.handlemax =" + jDebug.handlemax())
        log.debug("jDebug.handlecount =" + jDebug.handlecount())
        log.debug("========Handle信息:========")
        const startIndex = 1048575;
        let handleInfoObj: { [type: string]: { count: number, reference: number } } = {}
        for (let i = startIndex; i < startIndex + jDebug.handlemax(); i++) {
            let handledefInfo = handledef(i2h(i));
            if (!handledefInfo || !handledefInfo.type) {
                continue
            }
            let infoObj = handleInfoObj[handledefInfo.type]
            if (!infoObj) {
                infoObj = {count: 0, reference: 0};
            }
            infoObj.count++;
            if (handledefInfo.reference) {
                infoObj.reference += handledefInfo.reference;
            }
            handleInfoObj[handledefInfo.type] = infoObj;
        }
        for (let handleInfoObjKey in handleInfoObj) {
            let obj = handleInfoObj[handleInfoObjKey];
            log.debug(Develop.getTypeCnName(handleInfoObjKey) + "    数量: " + obj.count + "    引用计数: " + obj.reference)
        }


        log.debug("========Develop.showJassDebugInfo():End========")

    }


    static getTypeCnName(type: string): string {
        if (type == "+rct") {
            return "矩形区域"
        }
        if (type == "item") {
            return "物品"
        }
        if (type == "+trg") {
            return "触发器"
        }
        if (type == "+snd") {
            return "声音"
        }
        if (type == "+w3u") {
            return "单位"
        }
        if (type == "+loc") {
            return "点"
        }
        if (type == "+w3d") {
            return "地形装饰物"
        }

        return type;
    }


}
